import gtk

from Bases import BaseObject, Scaler

class Radio(BaseObject):
    def __init__(self, **kwargs):
        super(Radio, self).__init__(**kwargs)
        self.widget_signals = []
        self.attribute = kwargs.get('attribute')
        self.attribute.connect('value_changed', self.on_attribute_parameter_changed)
        self.widgets = {}
        self.widget_signals = {}
        self.setup_widgets()
        
    def setup_widgets(self):
        pass
        
    def unlink(self):
        self.attribute.disconnect(callback=self.on_attribute_value_changed)
        for key, val in self.widget_signals.iteritems():
            self.widgets[key].disconnect(val)
            
    def on_attribute_parameter_changed(self, **kwargs):
        key = kwargs.get('name')
        self.widgets[key].set_active(True)
            
    def on_widgets_clicked(self, widget):
        for key, val in self.widgets.iteritems():
            if val.get_active():
                self.attribute.set_parameter(key)

class Toggle(BaseObject):
    def __init__(self, **kwargs):
        super(Toggle, self).__init__(**kwargs)
        self.widget_signals = []
        self.src_object = kwargs.get('src_object')
        self.src_attr = kwargs.get('src_attr')
        self.toggled_by_program = False
        self.setup_widgets(**kwargs)
        self.widget.set_active(getattr(self.src_object, self.src_attr))
        
    def setup_widgets(self, **kwargs):
        pass
        
    def unlink(self):
        pass
            
    def on_object_value_changed(self, *args, **kwargs):
        state = getattr(self.src_object, self.src_attr)
        if state != self.widget.get_active():
            self.toggled_by_program = True
            self.widget.set_active(state)
            
    def on_widget_toggled(self, *args):
        if not self.toggled_by_program:
            setattr(self.src_object, self.src_attr, not getattr(self.src_object, self.src_attr))
        self.toggled_by_program = False

class Fader(BaseObject):
    def __init__(self, **kwargs):
        super(Fader, self).__init__(**kwargs)
        self.register_signal('obj_value_changed')
        self.attribute = kwargs.get('attribute')
        self.fader_drag = False
        self.widget_signals = []
        self.setup_widgets(**kwargs)
        if self.attribute.value is not None:
            self.set_widget_value(self.attribute.value)
        
        self.attribute.connect('value_changed', self.on_attribute_value_changed)
        
    def setup_widgets(self, **kwargs):
        pass
        
    def unlink(self):
        self.attribute.disconnect(callback=self.on_attribute_value_changed)
        for id in self.widget_signals:
            self.widget.disconnect(id)
            
    def on_widget_button_press(self, *args):
        self.fader_drag = True
    def on_widget_button_release(self, *args):
        self.fader_drag = False
    
    def on_widget_change_value(self, *args):
        self.attribute.value = self.get_widget_value()
    
    def on_attribute_value_changed(self, **kwargs):
        attrib = kwargs.get('attribute')
        value = kwargs.get('value')
        if attrib == self.attribute and self.fader_drag is False:
            self.set_widget_value(value)
            self.emit('obj_value_changed', attribute=self.attribute, obj=self)
        
    def set_widget_value(self, value):
        pass
    def get_widget_value(self):
        pass
    def set_widget_label(self, label):
        pass
        
class ScaledFader(BaseObject):
    def __init__(self, **kwargs):
        super(ScaledFader, self).__init__(**kwargs)
        
        self.register_signal('obj_value_changed')
        self.parameter = kwargs.get('parameter')
        
        uilog = False
        if self.parameter.units == 'dB':
            uilog = True
        self.ui_scale = {'min':self.parameter.scaled_min, 'max':self.parameter.scaled_max, 'LOG':uilog}
        self.param_scale = {'min':self.parameter.value_min, 'max':self.parameter.value_max}
        
        self.scaler = Scaler(scales={'ui':self.ui_scale, 'param':self.param_scale})
        
        self.fader_drag = False
        if self.parameter.value is not None:
            self.scaler.set_value('param', self.parameter.value)
        self.widget_signals = []
        self.setup_widgets(**kwargs)
        self.parameter.connect('value_update', self.on_parameter_value_update)
        self.scaler.connect('value_update', self.on_scaler_value_update)
    
    def setup_widgets(self, **kwargs):
        pass
        
    def unlink(self):
        self.parameter.disconnect(callback=self.on_parameter_value_update)
        for id in self.widget_signals:
            self.widget.disconnect(id)
            
    def on_widget_button_press(self, *args):
        self.fader_drag = True
    def on_widget_button_release(self, *args):
        self.fader_drag = False
    
    def on_widget_change_value(self, range, scroll, value):
        pass
    
    def on_parameter_value_update(self, **kwargs):
        param = kwargs.get('parameter')
        value = kwargs.get('value')
        if param == self.parameter and self.fader_drag is False:
            #print 'fader:', kwargs
            #self.adj.set_value(value)
            self.scaler.set_value('param', value)
        
    def set_widget_value(self, value):
        pass
        
    def on_scaler_value_update(self, **kwargs):
        name = kwargs.get('name')
        value = kwargs.get('value')
        if name == 'ui':
            self.set_widget_value(value)
        elif name == 'param':
            if int(value) != self.parameter.value:
                self.parameter.set_value(int(value))
                self.emit('obj_value_changed', parameter=self.parameter, obj=self)#, adj=self.adj)
                #print 'param: ', value
        #print 'adj: ', self.adj.get_value(), ', param: ', self.parameter.value

class Meter(BaseObject):
    def __init__(self, **kwargs):
        super(Meter, self).__init__(**kwargs)
        
